package cn.wolfcode.trip.admin.controller;

import cn.wolfcode.trip.admin.qiniu.SevenUploadUtil;
import cn.wolfcode.trip.admin.util.UploadUtil;
import cn.wolfcode.trip.base.domain.CreditShop;
import cn.wolfcode.trip.base.domain.ProductContent;
import cn.wolfcode.trip.base.domain.CreditShop;
import cn.wolfcode.trip.base.query.CreditShopQuery;
import cn.wolfcode.trip.base.service.ICreditShopService;
import cn.wolfcode.trip.base.util.JSONResult;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

@Controller
@RequestMapping("products")
public class CreditShopController {
    @Autowired
    private ICreditShopService creditShopService;
    //上架商品
    @RequestMapping("/putawayList")
    public String putawayList(@ModelAttribute("qo") CreditShopQuery qo, Model model){
        qo.setState(CreditShop.STATE_PUTAWAY);
        qo.setOrderBy("c.putawayTime desc");
        PageInfo pageInfo = creditShopService.selectForList(qo);
        model.addAttribute("result",pageInfo);
        return "creditShop/putawayList";
    }
    //推荐商品
    @RequestMapping("/recommendList")
    public String recommendList(@ModelAttribute("qo") CreditShopQuery qo, Model model){
        qo.setIsRecommend(1);
        qo.setOrderBy("c.putawayTime desc");
        PageInfo pageInfo = creditShopService.selectForList(qo);
        model.addAttribute("result",pageInfo);
        return "creditShop/recommendList";
    }
    //下架商品
    @RequestMapping("/soldoutList")
    public String soldoutList(@ModelAttribute("qo") CreditShopQuery qo, Model model){
        qo.setState(CreditShop.STATE_SOLDOUT);
        qo.setOrderBy("c.soldoutTime desc");
        PageInfo pageInfo = creditShopService.selectForList(qo);
        model.addAttribute("result",pageInfo);
        return "creditShop/soldoutList";
    }
    //待上架商品
    @RequestMapping("/waitList")
    public String waitList(@ModelAttribute("qo") CreditShopQuery qo, Model model){
        qo.setState(CreditShop.STATE_WAIT);
        qo.setOrderBy("c.createTime desc");
        PageInfo pageInfo = creditShopService.selectForList(qo);
        model.addAttribute("result",pageInfo);
        return "creditShop/waitList";
    }
    @RequestMapping("/getContent")
    @ResponseBody
    public Object getContent(Long productId){
        ProductContent data = creditShopService.getContent(productId);
        return creditShopService.getContent(productId);
    }

    //改变状态
    @RequestMapping("/changeState")
    @ResponseBody
    public JSONResult changeState(CreditShop creditShop){
        JSONResult result = new JSONResult();
        try {
            creditShopService.changeState(creditShop);
        }catch (Exception e){
            e.printStackTrace();
            result.mark("操作失败");
        }
        return result;
    }
    //增加库存
    @RequestMapping("/addCount")
    @ResponseBody
    public JSONResult addCount(Long productId, Long count){
        JSONResult result = new JSONResult();
        try {
            creditShopService.addCount(productId, count);
        }catch (Exception e){
            e.printStackTrace();
            result.mark("操作失败");
        }
        return result;
    }
    //更改是否推荐
    @RequestMapping("/changeCommend")
    @ResponseBody
    public JSONResult changeCommend(CreditShop creditShop){
        JSONResult result = new JSONResult();
        try {
            creditShopService.changeCommend(creditShop);
        }catch (Exception e){
            e.printStackTrace();
            result.mark("操作失败");
        }
        return result;
    }
    //待上架的编辑
    @RequestMapping("saveOrUpdate")
    @ResponseBody
    public JSONResult saveOrUpdate(CreditShop creditShop, MultipartFile file){
        JSONResult result = new JSONResult();
        try {
            if(file != null && file.getSize() > 0){
                String  imgUrl= SevenUploadUtil.uploadQiniu(file);
                creditShop.setCoverUrl(imgUrl);
            }
            creditShopService.saveOrUpdate(creditShop);
        }catch (Exception e){
            e.printStackTrace();
            result.mark(e.getMessage());
        }
        return result;
    }
}
